package com.novax.ex.activity.provider.controller;

import com.novax.ex.activity.infrastructure.entity.OperationConfig;
import com.novax.ex.activity.infrastructure.entity.OperationUserLog;
import com.novax.ex.activity.infrastructure.entity.OperationV2Config;
import com.novax.ex.activity.open.api.OperationUserLogApi;
import com.novax.ex.activity.open.api.OperationV2UserLogApi;
import com.novax.ex.activity.open.model.request.OperationUserLogRequest;
import com.novax.ex.activity.open.model.request.OperationV2UserLogRequest;
import com.novax.ex.activity.provider.service.OperationUserLogService;
import com.novax.ex.activity.provider.service.OperationV2UserLogService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @Description 活动落地页 热币 前台
 * @Author ChenXi
 * @Date 2023/04/18/10:08
 */

@RestController
public class OperationV2UserLogController extends BaseActivityController implements OperationV2UserLogApi {

    @Resource
    private OperationV2UserLogService operationV2UserLogService;

    @Override
    public ReturnResult<?> add(Long uid, String language, String ip, OperationV2UserLogRequest operationUserLogRequest,
                               BindingResult bindingResult) {
        // 缺少必填项
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        String customId = operationUserLogRequest.getCustomId();

        OperationV2Config operationV2Config = operationV2UserLogService.queryOperationIdByCustomId(customId);
        if(Objects.isNull(operationV2Config)){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_FOUND));
        }

        // 判断活动是否开启
        if(determineActiveStart(operationV2Config)){
            return ReturnResult.fail(getInternational(language, InternationalCode.ACTIVE_TIME_OR_STATUS));
        }

        // 查看是否已经报名
        Long i = operationV2UserLogService.selectUserIdAndCustomId(uid, customId);
        if(i >= 1){
            return ReturnResult.fail(getInternational(language, InternationalCode.RESUBMIT));
        }

        OperationUserLog operationUserLog = CopyUtils.copyObject(operationUserLogRequest, OperationUserLog.class);
        operationUserLog.setUserId(uid);
        operationUserLog.setOperationId(operationV2Config.getId());
        operationUserLog.setIp(ip);
        operationUserLog.setCreateTime(new Date());
        boolean res = operationV2UserLogService.addSelective(operationUserLog);
        return res ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> query(Long uid, String language, String customId) {
        Long i = operationV2UserLogService.selectUserIdAndCustomId(uid, customId);
        return ReturnResult.success(i > 0);
    }

    /**
     * @Description: 判断活动是否开启
     * @Author: ChenXi
     * @Date:  2023/4/20 18:15
     * @param operationV2Config
     * @return: boolean
     **/
    private boolean determineActiveStart(OperationV2Config operationV2Config) {
        Integer state = operationV2Config.getState();
        Date startTime = operationV2Config.getStartTime();
        Date endTime = operationV2Config.getEndTime();
        Date now = new Date();
        if(state != 1 || now.before(startTime) || now.after(endTime)){
            return true;
        }
        return false;
    }

}
